<?php
class AdAction extends CommonAction {
	private $allowId = array(); //不能删除的id
	
	public function __construct(){
		parent::__construct();
		
		$this->modelObject = D('Ad');
    }
	
	
	public function index(){
		$thisPage = $this->pageInfo['p'] = I('p', 1);
		
		if($this->pageInfo['name'] = I('name', '')){
			$map['name'] = array('LIKE', '%'.$this->pageInfo['name'].'%');
		}
		
		if($this->pageInfo['status'] = I('status', '')){
			$map['status'] = $this->pageInfo['status'];
		}
		
		//获得列表
		$list = $this->modelObject->getPageList($map, $thisPage);
		
		//分页
		$page = $this->modelObject->buildPage($map, $thisPage);
		$this->assign('page',$page);
		
		session('pageInfo', $this->pageInfo);
		$this->assign('pageInfo', $this->pageInfo);
		$this->assign('list', $list);
		
		$this->assign('allowId', $this->allowId);
		$this->display();
    }
	
	public function addPic(){
		$this->display();
	}
	public function addFlash(){
		$this->display();
	}
	public function addText(){
		$this->display();
	}
	
	public function add(){
		if(IS_POST){
			$this->insert();
		}
	}
	
	public function edit(){
		if(IS_POST){ //编辑操作
			$this->update();
		}elseif($id = I('id')){
			$info = $this->modelObject->getInfo($id);
			$this->assign('info', $info);
			switch($info['ad_type']){
				case 1:
					$this->display("editpic"); break;
				case 2:
					$pic      = explode('|', $info['flash']);
					$title    = explode('|', $info['flash_title']);
					$url      = explode('|', $info['flash_url']);
					$nofollow = explode('|', $info['nofollow']);
					$row_sort = explode('|', $info['row_sort']);
					$flashInfo = array();
					foreach($pic as $k=>$v){
						$flashInfo[$k]['flash'] = $pic[$k];	
						$flashInfo[$k]['flash_title'] = $title[$k];	
						$flashInfo[$k]['flash_url'] = $url[$k];	
						$flashInfo[$k]['nofollow'] = $nofollow[$k];	
						$flashInfo[$k]['row_sort'] = $row_sort[$k];	
					}
					$this->assign('flashInfo', $flashInfo);
					$this->display("editflash"); break;
				case 3:
					$this->display("edittext"); break;
				default:
					$this->error('参数错误');
			}
		}else{
			$this->error('参数错误');	
		}
	}
	
	/**
	 * 删除数据
	 * return ajax
	 */
	public function del(){
		if($id = I('id')){
			if(in_array($id, $this->allowId)){
				$data['status'] = 0;
				$data['message'] = L('del_error');
				$this->ajaxReturn($data);
			}
			
			$adInfo = $this->modelObject->getInfo($id); //获得图片
			
			if($this->modelObject->delete($id)){
				if($adInfo['ad_type'] == 1){
					//$pic = $uploadDir.$adInfo['pic'];
					//@unlink($pic);
					delImage($adInfo['pic']);
				}elseif($adInfo['ad_type'] == 2){
					$pic = explode('|', $adInfo['flash']);
					foreach($pic as $v){
						//@unlink($uploadDir.$v);	
						delImage($v);
					}
				}elseif($adInfo['ad_type'] == 3){
					delEditorImage($adInfo['text']);
				}
				$this->success('删除成功', U('index', session('pageInfo')));	
			}else{
				$this->error('删除失败!');
			}
		}else{
			$this->error('参数错误');	
		}
	}
	
	/*
	 * 删除所选
	 */
	public function delAll(){
		$idList = I('id');
		$deleteListId = array_filter($idList, array($this,'filterId'));
		$idStr = implode(',', $deleteListId);
		
		$adList = $this->modelObject->getAll(array('id'=>array('IN', $idStr)));
		if($this->modelObject->deleteInfo($idStr)){
			//$savePath = '.'. C('TMPL_PARSE_STRING.__UPLOADAD__');
			foreach($adList as $adInfo){
				if($adInfo['ad_type'] == 1){
					//$pic = $savePath.$adInfo['pic'];
					//@unlink($pic);
					delImage($adInfo['pic']);
				}elseif($adInfo['ad_type'] == 2){
					$pic = explode('|', $adInfo['flash']);
					foreach($pic as $v){
						//@unlink($savePath.$v);	
						delImage($v);
					}
				}elseif($adInfo['ad_type'] == 3){
					delEditorImage($adInfo['text']);
				}
			}
			$this->success('删除成功', U('index', session('pageInfo')));	
		}else{
			$this->error('删除失败!');
		}
	}
	/**
	* 过滤不能删除的id
	*/
	private function filterId($v){
		if(!in_array($v, $this->allowId)){
			return $v;	
		}
	}
	
	/**
	 * flash列表,单个图片删除
	 * return ajax
	 */
	public function delFlashPic(){
		if($id = I('id')){
			$info       = $this->modelObject->find($id);
			$pic        = explode('|', $info['flash']);
			$title      = explode('|', $info['flash_title']);
			$url        = explode('|', $info['flash_url']);
			$nofollow   = explode('|', $info['nofollow']);
			$row_sort   = explode('|', $info['row_sort']);
			$tempPic    = I('pic');
			foreach($pic as $k=>$v){
				if($v != $tempPic){
					$flash[]        = $pic[$k];	
					$flash_title[]  = $title[$k];	
					$flash_url[]    = $url[$k];	
					$nofollow[]     = $nofollow[$k];	
					$row_sort[]     = $row_sort[$k];	
				}
			}
			$data['id'] 	     = $id;
			$data['flash'] 	     = implode('|', $flash);
			$data['flash_title'] = implode('|', $flash_title);
			$data['flash_url']   = implode('|', $flash_url);
			$data['nofollow']    = implode('|', $nofollow);
			$data['row_sort']    = implode('|', $row_sort);
			
			if($this->modelObject->save($data)){
				$tempPic = I('pic');
				//@unlink('.'.C('TMPL_PARSE_STRING.__UPLOADAD__').$tempPic);
				delImage($tempPic);
				$re['status'] = 1;
				$this->ajaxReturn($re);	
			}else{
				$re['status'] = 0;
				$re['message'] = '删除失败';
				$this->ajaxReturn($re);	
			}
		}else{
			$re['status'] = 0;
			$re['message'] = '删除成功';
			$this->ajaxReturn($re);
		}
	}
	
	/**
	 * 插入广告信息
	 */
	private function insert(){
		$adType = $data['ad_type'] = I('ad_type');
		//插入数据的时候,如果没有上传图片广告,那么是不管标题是否有没写都是不能插入的.更新的时候就有可能只是想改一下title和url
		if($adType == 1){
			//$adType == 1 表示是图片广告,并且有pic上传,所以要清空flash轮播和text广告
			$data['pic'] = I('pic');
			$data['pic_url'] = I('pic_url');
			$data['pic_title'] = I('pic_title');
			$data['nofollow'] = I('nofollow');
			$data['flash'] = ''; 
			$data['flash_url'] = '';
			$data['flash_title'] = '';
			$data['text'] = ''; 
		}else if($adType == 2){ //如果 adType == 2则是flash轮播广告
			$data['pic'] = '';
			$data['pic_url'] = '';
			$data['pic_title'] = '';
			$data['text'] = ''; 
			$data['flash']       = implode('|',I('flash'));
			$data['flash_url']   = implode('|',I('flash_url'));
			$data['flash_title'] = implode('|',I('flash_title'));
			$data['nofollow']    = implode('|',I('nofollow'));
			$data['row_sort']    = implode('|',I('row_sort'));
		}else if($adType == 3){
			$data['pic'] = '';
			$data['pic_url'] = '';
			$data['pic_title'] = '';
			$data['flash'] = '';
			$data['flash_url'] = '';
			$data['flash_title'] = '';
			$data['nofollow'] = '';
			$data['text'] = I('text');
		}

		//获取公用数据
		$data['name']     = I('name');
		$data['remark']   = I('remark');
		$data['status']   = I('status',1);
		$data['add_time'] = NOW_TIME;
		
		if(empty($data['name'])){$this->error('广告标题必须填写!'); }
		
		if($insertId = $this->modelObject->add($data)){
			$this->modelObject->where('id ='.$insertId)->setField('get_code', "{:W('Ad', {$insertId})}");
			$this->success('增加成功', U('index'));	
		}else{
			$this->error('增加失败');		
		}
	}
	
	/**
	 * 更新广告
	 */
	private function update(){
		$adType = $data['ad_type'] = I('ad_type');
		$data['id'] = I('id');
		
		if($adType == 1){
			//删除七牛图片
			$tempPic = I('temp_pic');
			$pic     = I('pic');
			if($tempPic != $pic){
				delImage($tempPic);	
			}
			
			$data['pic'] = I('pic');
			$data['pic_url'] = I('pic_url');
			$data['pic_title'] = I('pic_title');
			$data['nofollow'] = I('nofollow');
			$data['flash'] = ''; 
			$data['flash_url'] = '';
			$data['flash_title'] = '';
			$data['text'] = ''; 
		}else if($adType == 2){ //如果 adType == 2则是flash轮播广告
			//删除七牛图片
			$tempPics = I('temp_pic');
			$pics     = I('flash');
			foreach($tempPics as $key=>$tempPic){
				if($tempPic != $pics[$key]){
					delImage($tempPic);	
				}
			}
			
			$data['pic'] = '';
			$data['pic_url'] = '';
			$data['pic_title'] = '';
			$data['text'] = ''; 
			$data['flash']       = implode('|',I('flash'));
			$data['flash_url']   = implode('|',I('flash_url'));
			$data['flash_title'] = implode('|',I('flash_title'));
			$data['nofollow']    = implode('|',I('nofollow'));
			$data['row_sort']    = implode('|',I('row_sort'));
		}else if($adType == 3){
			$data['pic'] = '';
			$data['pic_url'] = '';
			$data['pic_title'] = '';
			$data['flash'] = '';
			$data['flash_url'] = '';
			$data['flash_title'] = '';
			$data['nofollow'] = '';
			$data['text'] = I('text');
		}
		
		//获取公用数据
		$data['name'] = I('name');
		$data['remark'] = I('remark');
		$data['status'] = I('status',1);
		$data['add_time'] = NOW_TIME;
		// P($data);
		if(empty($data['name'])){$this->error('广告标题必须填写!'); }
		
		if($this->modelObject->save($data)){
			$this->success('编辑成功', U('index'));	
		}else{
			$this->error('编辑失败');		
		}
	}
}